/*
Copyright (c) 2010, Andri Yngvason
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of ArDAS nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>

#include "gpio.h"
#include "cmd.h"
#include "arduino/pins_arduino.h"


static void *gpio_cmd_list[] = {
	"set", &gpio_cmd_set,
	"get", &gpio_cmd_get,
	"ddr", &gpio_cmd_ddr,
	NULL
};


int gpio_ddr(int pin, int dir) {
	uint8_t m,p;
	volatile uint8_t *r;

	m = digitalPinToBitMask(pin);
	p = digitalPinToPort(pin);

	if(p == NOT_A_PIN)
		return 0;

	r = portModeRegister(p);

	if(dir)
		*r |= m;
	else
		*r &= ~m;

	return 1;
}


int gpio_set(int pin, int val) {
	uint8_t m,p;
	volatile uint8_t *r;

	m = digitalPinToBitMask(pin);
	p = digitalPinToPort(pin);

	if(p == NOT_A_PIN)
		return 0;

	r = portOutputRegister(p);

	if(val)
		*r |= m;
	else
		*r &= ~m;

	return 1;
}


int gpio_get(int pin) {
	uint8_t m,p;
	volatile uint8_t *r;

	m = digitalPinToBitMask(pin);
	p = digitalPinToPort(pin);

	if(p == NOT_A_PIN)
		return -1;

	r = portInputRegister(p);
	
	return !!(*r & m);
}


int gpio_cmd(char *line) {
	int (*f)(char*);
	f = cmd_find(&line,gpio_cmd_list,&strcmp);

	if(f == cmd_notfound) {
		printf("ERROR: GPIO command not found.\n");
		return 1;
	}

	if(f == NULL) {
		return 0;
	}

	return f(line);
}


int gpio_cmd_set(char *line) {
	char *pin;
	char *val;

	pin = strsep(&line, " \n\r");
	val = strsep(&line, " \n\r");
	if(pin == NULL || val == NULL) {
		printf("ERROR: Please specify pin and value.\n");
		return 1;
	}

	if(!gpio_set(atoi(pin), atoi(val))) {
		printf("ERROR: Invalid pin.");
		return 1;
	}

	return 0;
}


int gpio_cmd_get(char *line) {
	char *pin;
	int data;

	pin = strsep(&line, " \n\r");
	if(pin == NULL) {
		printf("ERROR: Please specify pin.\n");
		return 1;
	}

	data = gpio_get(atoi(pin));

	if(data < 0) {
		printf("ERROR: Invalid pin.");
		return 1;
	}

	printf("GPIO[%s]: %d\n", pin, data);

	return 0;
}


int gpio_cmd_ddr(char *line) {
	char *pin;
	char *val;

	pin = strsep(&line, " \n\r");
	val = strsep(&line, " \n\r");
	if(pin == NULL || val == NULL) {
		printf("ERROR: Please specify pin and value.\n");
		return 1;
	}

	if(!gpio_ddr(atoi(pin), atoi(val))) {
		printf("ERROR: Invalid pin.");
		return 1;
	}

	return 0;
}
